package com.jinsi.controller.lv;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinsi.pojo.Order;
import com.jinsi.pojo.OrderList;
import com.jinsi.service.lv.OrderService;
import com.jinsi.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 吕昊燃
 * @Date 2022/10/12
 * 订单列表
 */
@Controller
@RequestMapping("/order")
@Slf4j
public class OrderController {
//    private final static Logger LOG = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private OrderService orderService;

    /**
     * 根据传进来的参数对商品订单进行模糊查询
     * @param page
     * @param limit
     * @param order
     * @return
     */
    @ResponseBody
    @RequestMapping("/getOrder")
    public R getOrder(Integer page, Integer limit, Order order){
        PageHelper.startPage(page,limit);
        log.info("根据获取订单数据[开始],order:{}",order);
        List<Order> all = orderService.getOrder(order);
        log.info("根据获取订单数据[结束],all:{}", all);
        PageInfo<Order> pageInfo = new PageInfo<>(all);
        return R.custom(0,"成功",pageInfo.getList(),pageInfo.getTotal());
    }


    /**
     * 商品订单进行地址修改
     * @param orderId
     * @param orderSite
     * @param orderCity
     * @param orderDistrict
     * @param orderDetail
     * @return
     */
    @ResponseBody
    @RequestMapping("/getOrderSiteAmend")
    public R getOrderSiteAmend(Integer orderId,String orderSite,String orderCity,String orderDistrict,String orderDetail) {
        log.info("修改订单地址[开始],orderId:{}",orderId);
        orderService.getOrderSiteAmend(orderId,orderSite,orderCity,orderDistrict,orderDetail);
        Map<String,Object> map = new HashMap<>();
        map.put("tip", "成功");
        log.info("修改订单地址[结束],map:{}",map);
        return R.ok(map);
    }

    /**提交已发货状态
     *
     * @param orderId
     * @return
     */
    @ResponseBody
    @GetMapping("/intermediate/confirm")
    public R confirm(int orderId){
        log.info("订单发货接口[开始], orderId:{}", orderId);
        orderService.confirm(orderId);
        boolean result = true;
        log.info("订单发货接口[结束], result:{}", result);
        return R.ok("成功");
    }

    /**
     * 修改地址
     * @param order
     * @return
     */
    @ResponseBody
    @RequestMapping("/intermediate/addRess")
    public R addRess(Order order){
        log.info("修改地址[开始],order:{}", order);
        orderService.addRess(order);
        boolean result = true;
        log.info("修改地址[结束]", result);
        return R.ok("成功");
    }

    /**
     * 查看课程订单
     * @param page
     * @param limit
     * @param order
     * @return
     */
    @ResponseBody
    @GetMapping("/intermediate/getCurr")
    public R getCurr(Integer page, Integer limit,Order order){
        PageHelper.startPage(page,limit);
        log.info("查看课程订单[开始],order:{}",order);
        List<Order> all =orderService.getCurr(order);
        boolean result = true;
        log.info("查看课程订单[结束],all:{}", all);
        PageInfo<Order> pageInfo = new PageInfo<>(all);
        return R.custom(0,"成功",pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 根据id查看课程详细信息
     * @param orderId
     * @param model
     * @return
     */
    @RequestMapping("/intermediate/viewCourses")
    public String viewCourses(Integer orderId, Model model){
        log.info("查看课程详情[开始],orderId:{}",orderId);
        Order viewCourses = orderService.viewCourses(orderId);
        boolean result = true;
        log.info("查看课程详情[结束]", result);
        model.addAttribute("viewcourses",viewCourses);
        return "pages/order/curriculum/viewCourses";
    }

    /**
     *  查看退货订单
     * @param page
     * @param limit
     * @param orderStatus
     * @param order
     * @return
     */
    @ResponseBody
    @GetMapping("/getreturnList")
    public R getreturnList(Integer page, Integer limit, @RequestParam("orderStatus[]")List<Integer> orderStatus, Order order){

        PageHelper.startPage(page,limit);
        log.info("查看退货订单[开始],order:{}",order);
        List<Order> getReturnList = orderService.getReturnList(orderStatus,order);
        log.info("查看退货订单[结束],getReturnList:{}", getReturnList);
        PageInfo<Order> pageInfo = new PageInfo<>(getReturnList);
        return R.custom(0,"成功",pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 订单统计
     * @return
     */
    @ResponseBody
    @GetMapping("/census")
    public R census(){
        HashMap<String, Object> map = new HashMap<>();
        log.info("订单统计[开始]");
        List<OrderList> courseCensus = orderService.courseCensus();
        List<OrderList> goodsCensus = orderService.goodsCensus();
        List<OrderList> refundCensus = orderService.refundCensus();
        map.put("courseCensus",courseCensus);
        map.put("goodsCensus",goodsCensus);
        map.put("refundCensus",refundCensus);
        log.info("订单统计[结束],map:{}", map);
        return R.ok(map);
    }
}
